package cn.house.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import cn.house.dto.LoginUser;
import cn.house.dto.WebLoginUser;
import cn.house.service.UserService;

/**
 * 实现UserDetailsService，到数据库查找信息
 * 
 * @author 偶尔有点困
 * @date 2018年5月21日
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {

	@Autowired
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		if (StringUtils.isBlank(username)) {
			throw new IllegalArgumentException("不合法参数");
		}
		String[] uStrings = username.split("\\|");
		if (uStrings.length != 2) {
			throw new IllegalArgumentException("不合法参数");
		}
		LoginUser loginUser = new LoginUser();
		WebLoginUser webLoginUser = new WebLoginUser();
		if (uStrings[0].equals("ADMIN")) {
			loginUser = userService.selectByUsername(uStrings[1]);
			return loginUser;
		} else if (uStrings[1].equals("WEB")) {
			webLoginUser = userService.selectWebUserByUsername(uStrings[1]);
			return webLoginUser;
		}
		throw new UsernameNotFoundException("用户名或密码错误");
	}

}